<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Hash;
use Mail;
class UsermessageController extends Controller
{
	//个人中心
	public function getIndex()
	{
		$name = session('uname');
		$res = DB::table('shop_users')->where('uname',$name)->first();
		$status = $res->status;
		$mail = $res->email;
		return view('home.member.usermessage',['status'=>$status,'mail'=>$mail]);
	}

	/*
		密码验证
	*/
	public function postEditpw(Request $request)
	{
		$name = session('uname');

		$data = $request->except('_token');
		//非空以及格式判断
		if(empty($data['newupwd'])){
			// return back()->with('error','密码不能为空');
			return redirect('/home/umessage/index')->with('error','密码不能为空');
			
		}
			
		if(!preg_match("/\w{6,18}/",$data['newupwd'])){
			// return back()->with('error','密码格式不正确');
			return redirect('/home/umessage/index')->with('error','密码格式不正确');
		}
		if($data['newupwds'] != $data['newupwd']){
			// return back()->with('error','两次输入密码不能为空');
			return redirect('/home/umessage/index')->with('error','两次密码不一致');
		}

		//加密
		$data['newupwd'] = Hash::make($data['newupwd']);
		$arr = ['upwd'=>$data['newupwd']] ;
		$res = DB::table('shop_users')->where('uname', $name)->update($arr);
		if($res){
   			//成功
   			$request->session()->flush();
   			return redirect('/')->with('success','修改成功');
   		}else{
   			//失败
   			return redirect('/home/umessage/index')->with('error','修改失败');
		}
	}
	
	/*
		接收ajax 发送
	*/
	public function getSendmails(Request $request)
	{
		$data = $request->only(['email']);
		$mail = DB::table('shop_users')->where('email',$data['email'])->first();
		if($mail){
			echo 0;
		}else{
			$data['updtime'] = time();
			$name = session('uname');
			$res = DB::table('shop_users')->where('uname',$name)->first();
			$id = $res->uid;
			$token = $res->token;
			$this->getSendmail($id,$token,$data['email']);
			echo 1;
		}
	}

	/*
		发送邮件
	*/
	public function getSendmail($id,$token,$email){
		
		//发送模板
		Mail::send('home.email.index', ['id'=>$id,'token'=>$token,'email'=>$email], function ($message)use($email) {
        	$message->to($email)->subject('激活邮件');
    	});

	}

	/*
		邮箱激活
	*/
	public function getJihuo(Request $request)
	{
		$id = $request->input('id');
		$email = $request->input('email');
		//查询用户
		$user = DB::table('shop_users')->where('uid',$id)->first();
		if(empty($user)){
			return '链接失效,未查询到该用户';
		}
		//看请求是否合法
		if($request->input('token') != $user->token){
			return '链接失效，非法请求';
		}
		//修改状态 重置token
		$data = [
			'status' =>1,
			'token' =>str_random(50),
			'email' =>$email
		];
		//执行修改
		$res = DB::table('shop_users')->where('uid',$id)->update($data);
		if($res){
			return redirect('/')->with('success','激活成功');
		}else{
			return redirect('/')->with('error','激活失败');
		}
	}

	/*
		个人资料管理页面
	*/
	public function getMember()
	{
		return view('home.member.member');
	}

	/*
		验证码方法
	*/
	public function getVcode()
	{
		ob_clean();//清除输出缓存区的内容
	    //生成验证码图片的Builder对象，配置相应属性
	    $builder = new CaptchaBuilder;
	    //可以设置图片宽高及字体
	    $builder->build($width = 100, $height = 40, $font = null);
	    //获取验证码的内容
	    $phrase = $builder->getPhrase();

	    //把内容存入session
	    session(['Vcode'=>$phrase]);
	    //生成图片
	    header("Cache-Control: no-cache, must-revalidate");
	    header('Content-Type: image/jpeg');
	    $builder->output();

	}

	/*
		验证验证码
	*/
	public function getCode(Request $request)
	{

		//验证码是否正确
		if($request->input('auth_code') == session('Vcode')){
			echo 1;
		}
	}
	
	/*
		提交留言
	*/
	public function postSave(Request $request)
	{
		
		$data = $request->only(['type','jianyi']);
		$data['uname'] = session('uname');
		$data['regtime'] = time();
		$res = DB::table('shop_jianyi')->insert($data);
		

	}

	
	/*
		留言
	*/
	public function getLiuyan(Request $request)
	{
		//获取参数列表
		$list = $request->all();
		$uname = session('uname');
	
		$liuyan = DB::table('shop_jianyi')->where('uname',$uname)->paginate(10); 
		foreach($liuyan as $k=>$v){
			$v->regtime = date('Y-m-d H:i:s',$v->regtime);
		}
		return view('home.member.liuyan',['liuyan'=>$liuyan,'list'=>$list]);
	}


	/*
		留言删除
	*/
	public function getDelete(Request $request)
	{
		//获取id
		$id = $request->input('id');

		//删除数据库
		$res = DB::table('shop_jianyi')->where('id',$id)->delete();
		//返回给ajax
		echo $res;
	}

}
